<script setup>
import { ref, onMounted } from 'vue'
import * as echarts from 'echarts'

// 模拟用户统计数据
const userStats = ref({
  totalUsers: 12542,
  vipUsers: 2541,
  adminUsers: 48,
  frozenUsers: 3214,
  newUsersToday: 347
})

// 用户增长数据
const growthData = ref([
  { date: '6/18', users: 120 },
  { date: '6/19', users: 135 },
  { date: '6/20', users: 148 },
  { date: '6/21', users: 216 },
  { date: '6/22', users: 194 },
  { date: '6/23', users: 347 }
])

// 角色分布数据
const roleDistribution = ref([
  { name: '普通会员', value: 8562 },
  { name: 'VIP会员', value: 2541 },
  { name: '管理员', value: 1439 }
])

// 用户状态数据
const userStatus = ref([
  { name: '启用用户', value: 9328 },
  { name: '冻结用户', value: 3214 }
])

// 权限层级数据
const permissionHierarchy = ref({
  name: '权限系统',
  children: [
    {
      name: '系统管理',
      children: [
        { name: '用户管理', value: 12 },
        { name: '角色管理', value: 8 },
        { name: '权限管理', value: 10 }
      ]
    },
    {
      name: '业务管理',
      children: [
        { name: '订单管理', value: 6 },
        { name: '支付管理', value: 7 },
        { name: '物流管理', value: 5 }
      ]
    },
    {
      name: '内容管理',
      children: [
        { name: '文章管理', value: 9 },
        { name: '评论管理', value: 4 },
        { name: '审核管理', value: 6 }
      ]
    }
  ]
})

// 角色权限分布
const rolePermissions = ref([
  { role: '超级管理员', permissions: 35 },
  { role: '普通管理员', permissions: 67 },
  { role: 'VIP会员', permissions: 266 },
  { role: '普通会员', permissions: 300 }
])

// 初始化图表
onMounted(() => {
  // 用户增长趋势图
  const growthChart = echarts.init(document.getElementById('growth-chart'))
  growthChart.setOption({
    tooltip: {
      trigger: 'axis'
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: growthData.value.map(item => item.date)
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '新增用户',
        type: 'line',
        smooth: true,
        lineStyle: {
          width: 4,
          color: '#5B8FF9'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(91, 143, 249, 0.5)' },
            { offset: 1, color: 'rgba(91, 143, 249, 0.1)' }
          ])
        },
        data: growthData.value.map(item => item.users)
      }
    ]
  })

  // 角色分布图
  const roleChart = echarts.init(document.getElementById('role-chart'))
  roleChart.setOption({
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      right: 10,
      top: 'center',
      data: roleDistribution.value.map(item => item.name)
    },
    series: [
      {
        name: '用户角色',
        type: 'pie',
        radius: ['40%', '70%'],
        center: ['40%', '50%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2,
          // 添加颜色回调函数
          color: function(params) {
            // 根据名称设置颜色
            const colorMap = {
              '管理员': '#67C23A',  // 绿色
              'VIP会员': '#FFFF00',     // 明黄色
              // 其他角色默认颜色
              'default': '#378add'  // 默认蓝色
            };
            return colorMap[params.name] || colorMap['default'];
          }
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: '18',
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: roleDistribution.value
      }
    ]
  })

  // 用户状态图
  const statusChart = echarts.init(document.getElementById('status-chart'))
  statusChart.setOption({
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      bottom: 10,
      left: 'center',
      data: userStatus.value.map(item => item.name)
    },
    series: [
      {
        name: '用户状态',
        type: 'pie',
        radius: '70%',
        center: ['50%', '45%'],
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2,
		  color: function(params) {
		    // 根据名称设置颜色
		    const colorMap = {
		      '冻结用户': '#ffaf2e',  // 
		      
		      'default': '#378add'  // 默认蓝色
		    };
		    return colorMap[params.name] || colorMap['default'];
		  }
        },
        label: {
          show: true,
          formatter: '{b}: {c} ({d}%)'
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        },
        data: userStatus.value
      }
    ]
  })

  // 权限层级图
  const permissionChart = echarts.init(document.getElementById('permission-chart'))
  permissionChart.setOption({
    tooltip: {
      trigger: 'item',
      triggerOn: 'mousemove',
      formatter: '{b}: {c}'
    },
    series: [
      {
        type: 'tree',
        data: [permissionHierarchy.value],
        top: '10px',
        left: '8%',
        bottom: '8%',
        right: '20%',
        symbolSize: 7,
        label: {
          position: 'left',
          verticalAlign: 'middle',
          align: 'right',
          fontSize: 14
        },
        leaves: {
          label: {
            position: 'right',
            verticalAlign: 'middle',
            align: 'left'
          }
        },
        expandAndCollapse: true,
        animationDuration: 550,
        animationDurationUpdate: 750
      }
    ]
  })

  // 角色权限分布图
  const rolePermissionChart = echarts.init(document.getElementById('role-permission-chart'))
  rolePermissionChart.setOption({
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'value',
      boundaryGap: [0, 0.01]
    },
    yAxis: {
      type: 'category',
      data: rolePermissions.value.map(item => item.role)
    },
    series: [
      {
        name: '权限数量',
        type: 'bar',
        data: rolePermissions.value.map(item => item.permissions),
        itemStyle: {
          color: function(params) {
            const colorList = ['#ee6666', '#91cc75', '#f5f500', '#5470c6'];
            return colorList[params.dataIndex % colorList.length];
          }
        }
      }
    ]
  })

  // 响应式调整
  window.addEventListener('resize', () => {
    growthChart.resize()
    roleChart.resize()
    statusChart.resize()
    permissionChart.resize()
    rolePermissionChart.resize()
  })
})
</script>

<template>
  <div class="user-summary-container">


    <!-- 关键指标卡片 -->
    <div class="stats-cards">
      <div class="stats-card">
        <div class="card-icon bg-apricot">
          <i class="el-icon-user"></i>
        </div>
        <div class="card-content">
          <div class="card-title">总用户数</div>
          <div class="card-value">{{ userStats.totalUsers.toLocaleString() }}</div>
          <div class="card-change">较上月增长 12.7%</div>
        </div>
      </div>

      <div class="stats-card">
        <div class="card-icon bg-yellow">
          <i class="el-icon-star-on"></i>
        </div>
        <div class="card-content">
          <div class="card-title">VIP会员</div>
          <div class="card-value">{{ userStats.vipUsers.toLocaleString() }}</div>
          <div class="card-change">会员转化率 20.3%</div>
        </div>
      </div>

      <div class="stats-card">
        <div class="card-icon bg-green">
          <i class="el-icon-s-custom"></i>
        </div>
        <div class="card-content">
          <div class="card-title">管理员</div>
          <div class="card-value">{{ userStats.adminUsers.toLocaleString() }}</div>
          <div class="card-change">超级管理员: 12</div>
        </div>
      </div>

      <div class="stats-card">
        <div class="card-icon bg-orange">
          <i class="el-icon-lock"></i>
        </div>
        <div class="card-content">
          <div class="card-title">冻结用户</div>
          <div class="card-value">{{ userStats.frozenUsers.toLocaleString() }}</div>
          <div class="card-change">今日新增冻结: 24</div>
        </div>
      </div>
    </div>

    <!-- 图表区域 -->
    <div class="charts-container">
      <div class="chart-row">
        <div class="chart-card">
          <div class="chart-header">
            <h3><i class="el-icon-trend-charts"></i> 用户增长趋势</h3>
            <div class="chart-subtitle">近7日新增用户数据</div>
          </div>
          <div id="growth-chart" class="chart"></div>
        </div>

        <div class="chart-card">
          <div class="chart-header">
            <h3><i class="el-icon-s-data"></i> 用户角色分布</h3>
            <div class="chart-subtitle">各角色用户占比情况</div>
          </div>
          <div id="role-chart" class="chart"></div>
        </div>

        <div class="chart-card">
          <div class="chart-header">
            <h3><i class="el-icon-s-flag"></i> 用户状态分布</h3>
            <div class="chart-subtitle">启用与冻结用户比例</div>
          </div>
          <div id="status-chart" class="chart"></div>
        </div>
      </div>

      <div class="chart-row">
        <div class="chart-card">
          <div class="chart-header">
            <h3><i class="el-icon-s-operation"></i> 权限层级结构</h3>
            <div class="chart-subtitle">系统权限组织结构</div>
          </div>
          <div id="permission-chart" class="chart"></div>
        </div>

        <div class="chart-card">
          <div class="chart-header">
            <h3><i class="el-icon-s-claim"></i> 角色权限分布</h3>
            <div class="chart-subtitle">不同角色的权限数量</div>
          </div>
          <div id="role-permission-chart" class="chart"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.user-summary-container {
  padding: 24px;
  background-color: #f5f7fa;
  min-height: 100vh;
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

.header {
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #ebeef5;
}

.header h1 {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.subtitle {
  font-size: 14px;
  color: #909399;
  font-style: italic;
}

.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stats-card {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  padding: 20px;
  display: flex;
  transition: all 0.3s ease;
  border-top: 3px solid transparent;
}

.stats-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
}

.card-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  font-size: 28px;
  color: white;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.bg-blue {
  background: linear-gradient(135deg, #409eff, #337ecc);
  border-top-color: #409eff;
}

.bg-green {
  background-color: #67C23A;
}

.bg-orange {
  background-color: #ffaf2e;
}

.bg-apricot {
  background-color: #FBCEB1; /* 这是标准的杏色色值 */
  /* 例如: #FFD8B1, #FDD5B1, #FBB982 等 */
}
.bg-yellow{
	background-color: #f5f500;
}
.bg-purple {
  background: linear-gradient(135deg, #a050f0, #7d3cc4);
  border-top-color: #a050f0;
}

.card-content {
  flex: 1;
}

.card-title {
  font-size: 14px;
  color: #606266;
  margin-bottom: 4px;
  font-weight: 500;
}

.card-value {
  font-size: 26px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 4px;
  letter-spacing: 0.5px;
}

.card-change {
  font-size: 13px;
  color: #67c23a;
  font-weight: 500;
}

.charts-container {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.chart-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 24px;
}

.chart-card {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  padding: 20px;
  height: 400px;
  display: flex;
  flex-direction: column;
  transition: all 0.3s ease;
}

.chart-card:hover {
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
}

.chart-header {
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #ebeef5;
}

.chart-header h3 {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.chart-subtitle {
  font-size: 13px;
  color: #909399;
}

.chart {
  flex: 1;
  min-height: 300px;
}

@media (max-width: 1200px) {
  .chart-row {
    grid-template-columns: 1fr;
  }

  .chart-card {
    height: 350px;
  }
}

@media (max-width: 768px) {
  .stats-cards {
    grid-template-columns: 1fr;
  }

  .chart-card {
    height: 320px;
  }
}
</style>